/**
 * neededRole: access role,
 * role: current role
 */
import { ENUM } from 'utils/constants.js';

function access(neededRole, role) {
  return neededRole.includes(role);
}

/**
 * 检查是否保护分支，保护分支限制的用户不允许push
 * @param protectBranch
 * @param role
 * @returns {boolean}
 */
function protectBranchCheck(protectBranch, role) {
  const { access_level_id } = ENUM;
  const accessRole = access_level_id[role];
  if (protectBranch === '{}' || !protectBranch) {
    return true;
  }
  if (typeof protectBranch === 'object' && JSON.stringify(protectBranch) === '{}') {
    return true;
  }
  let allowPush = false;
  const { allow_push = [] } = protectBranch;
  if (protectBranch?.allow_push_user_ids?.includes(Number(window.FORCE.user.codeUserId))) {
    return true;
  }
  allow_push.forEach((allowRole) => {
    if (accessRole >= allowRole) {
      allowPush = true;
    }
  });

  return allowPush;
}

export { access, protectBranchCheck };
